<template>
	<!--登录 -->
	<view class="page-content">
		<!-- 标题栏和状态栏占位符 -->
		<view class="titleNview-placing"></view>
		<view class="page-top">
			<view class="top-txt">您好！</view>
			<view class="top-txt">欢迎登录铛铛</view>
		</view>
		<view class="page-body">
				<!-- 登录输入界面 -->
				<view class="login_input_box">
					<view class="input-group">
						<view class="input-row">
							<image class="icon-tb" src="/static/login/shouji.png"></image>
							<input class="input-row-w" type="number" v-model="params.mobile" placeholder="输入手机号"
								 placeholder-style="color:#999"></input>
						</view>
						<view class="input-row">
							<image class="icon-tb" src="/static/login/mima.png"></image>
							<input class="input-row-w" type="password" v-model="params.pass" placeholder="输入密码"
								 displayable="true" placeholder-style="color:#999"></input>
						</view>
						<view class="psd-reg">
							<view class="" @click="toregister">快速注册</view>
							<view class="" @click="toforget">忘记密码？</view>
						</view>
						<view class="btn-row">
							<button type="primary" class="" @tap="bindLogin">登录</button>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapActions,
		mapState
	} from 'vuex'
	import {
		sendSmss,
		logins,
		registers,
		getUserInfos,
		tencentImAuths, //腾讯云IM登录鉴权
		getUserCompanys, //获取当前公司
		getMailLists, //获取通讯录
	} from '@/common/api.js';
	import  store  from '@/store'
	export default {
		components: {
		},
		data() {
			return {
				params: {
					mobile: '',
					pass: ''
				},
				formdata:{
					mobile: '',
					sms_code:'',
					pass: '',
					type:'3344',
					sms_type:'3344'
				},
				currentSubIndex:0,
				noClick:true
			}
		},
		computed: {
			...mapGetters(['userSig','selIndex']),
		},
		onLoad(query) {
			console.log(1234)
			this.params.mobile = '17689692341';//'17689692341'; 18120779920 17700084288 17700084283
			this.params.pass = '654321'; //123456 //654321
			this.formdata.sms_code = '9999';
		},
		methods: {
			...mapActions('user',['setToken','setUserInfo','setUserSig','setCompanyInfo','setSelIndex','setCompanyRole','setUserRole','setContactList']),
			//登录
			async bindLogin() {
				let _self = this;
				if (_self.noClick) {
				    _self.noClick= false;
				    setTimeout(function () {
				        _self.noClick= true;
				    }, 2000)
				} else {
				    console.log("请稍后点击")
				}
				
				let resdata = this.params;
				if (resdata.mobile === "") {
					uni.showToast({
						title: "手机号码不能为空！",
						icon: 'none',
						duration: 2000
					})
					return;
				}
				if (!/(^1[3|4|5|7|8][0-9]{9}$)/.test(resdata.mobile)) {
					uni.showToast({
						title: "请输入正确的手机号码！",
						icon: 'none',
						duration: 2000
					})
					return;
				}
				if (resdata.pass === "") {
					uni.showToast({
						title: "请输入密码！",
						icon: 'none',
						duration: 2000
					})
					return;
				}
				//登录
				let resdata1 = await logins(resdata);
				if(resdata1.code === 2000){
					_self.setToken(resdata1.data.token)
					//获取公司信息
					let resdata2 = await getUserCompanys();
					console.log(111222,resdata2)
					if(resdata2.code === 2000){
						if(resdata2.data && resdata2.data.length>0){ //有公司品牌存在
							let currentSubIndex = 0
							/* if(!!this.selIndex){
								currentSubIndex = parseInt(this.selIndex);
							}else{
								_self.setSelIndex(currentSubIndex);
							} */
							_self.setSelIndex(currentSubIndex);
							_self.setCompanyInfo(resdata2.data[currentSubIndex]);
							
							 //获取用户信息
							let resdata3 = await getUserInfos({company_id: resdata2.data[currentSubIndex].id})
							_self.setUserInfo(resdata3.data);
							_self.getCompanyRole(resdata2.data[currentSubIndex],resdata3.data);
							
							//获取im-userSig
							let resdata4 = await tencentImAuths({company_id:resdata2.data[currentSubIndex].id}) 
							_self.setUserSig(resdata4.data.user_sig)
							
							//im-登录
							let promise = _self.tim.login({
								userID: resdata2.data[currentSubIndex].id + ':' + resdata3.data.uid+'',
								userSig: resdata4.data.user_sig
							});
							let userInfo = {
								userID:resdata2.data[currentSubIndex].id + ':' + resdata3.data.uid+'',
								userSig: resdata4.data.user_sig
							};
							
							// 获取公司通讯录
							let params1 = { company_id:resdata2.data[currentSubIndex].id,type:1}
							let params2 = { company_id:resdata2.data[currentSubIndex].id,type:2}
							let resdata6 = await getMailLists(params1);
							let resdata7 = await getMailLists(params2);
							let contact = [...resdata6.data, ...resdata7.data]
							_self.setContactList(contact);
							
							promise.then((res5) => {
								console.log('im登录成功！', res5)
								//登录成功后 更新登录状态
								//_self.$store.commit("toggleIsLogin", true);
								//自己平台的用户基础信息
								uni.setStorageSync('userInfo', JSON.stringify(userInfo))
								//tim 返回的用户信息
								uni.setStorageSync('userTIMInfo', JSON.stringify(res5.data))
								uni.switchTab({
									url: '/pages/index/index',
								});
							}).catch((err) => {
								console.warn('login error:', err); // 登录失败的相关信息
							});
							
						}else{ //公司没有品牌跳转 创建品牌页面
							uni.navigateTo({
								url:`/pages/brandCreation/companyNo?id=`
							})
						}
					}else{
						uni.showToast({title:resdata2.msg,icon:'none',duration: 2000})
					}
				}else{
					uni.showToast({title:resdata1.msg,icon:'none',duration: 2000})
				}
			},
			getCompanyRole(companyInfo,userInfo){
				console.log('获取公司权限')
				let companyRole = {
					bas_role:true,
					team_role:true,
					news_role:true,
					pop_role:false,
					pro_role:false,
					prop_role:false,
				}
				let userRole = {
					proStatu:false, //产品属性设置-1
					popStatu:false, //POP编码设置-2
					propStatu:false, //道具编码设置-3
					textureStatu:false, //POP材质设置-4
					quotationStatu:false, //供应商报价单-5
					
					memberStatu:false, //成员管理-6
					groupStatu:false, //组别管理-7
					powerStatu:false, //权限管理-8
					
					newsStatu:false, //新闻管理-9
					noticeStatu:false, //公告&通知管理-10
					dynamicStatu:false, //动态管理-11
					
					proOrderStatu:false, //POP画册管理-12
					
					popOrderStatu:false , //POP下单管理-13
					countStatu1:false , //数据统计功能-14
					
					propOrderStatu:false , //POP下单管理-15
					countStatu2:false , //数据统计功能-16
				}
				let role_arr = []
				if(userInfo){
					if(userInfo.role_arr && userInfo.role_arr.length>0){
						role_arr = userInfo.role_arr
					}
				}
				if(companyInfo){
					if(companyInfo.roleArr && companyInfo.roleArr.length>0){
						companyInfo.roleArr.forEach(item=>{
							if(item === '1'){
								companyRole.bas_role = true;
								if(userInfo.type === 0){
									userRole.proStatu = true;
									userRole.popStatu = true;
									userRole.powerStatu = true;
									userRole.textureStatu = true;
									userRole.quotationStatu = true;
								}else if(userInfo.type === 1){
									role_arr.forEach(ite=>{
										if(parseInt(ite) === 1){
											userRole.proStatu = true;
										}else if(parseInt(ite) === 2){
											userRole.popStatu = true;
										}else if(parseInt(ite) === 3){
											userRole.powerStatu = true;
										}else if(parseInt(ite) === 4){
											userRole.textureStatu = true;
										}else if(parseInt(ite) === 5){
											userRole.quotationStatu = true;
										}
									})
								}
							}else if(item === '2'){
								companyRole.team_role = true;
								if(userInfo.type === 0){
									userRole.memberStatu = true;
									userRole.groupStatu = true;
									userRole.propStatu = true;
								}else if(userInfo.type === 1){
									role_arr.forEach(ite=>{
										if(parseInt(ite) === 6){
											userRole.memberStatu = true;
										}else if(parseInt(ite) === 7){
											userRole.groupStatu = true;
										}else if(parseInt(ite) === 8){
											userRole.propStatu = true;
										}
									})
								}
							}else if(item === '3'){
								companyRole.news_role = true;
								if(userInfo.type === 0){
									userRole.newsStatu = true;
									userRole.noticeStatu = true;
									userRole.dynamicStatu = true;
								}else if(userInfo.type === 1){
									role_arr.forEach(ite=>{
										if(parseInt(ite) === 9){
											userRole.newsStatu = true;
										}else if(parseInt(ite) === 10){
											userRole.noticeStatu = true;
										}else if(parseInt(ite) === 11){
											userRole.dynamicStatu = true;
										}
									})
								}
							}else if(item === '4'){
								companyRole.pro_role = true;
								if(userInfo.type === 0){
									userRole.proOrderStatu = true;
								}else if(userInfo.type === 1){
									role_arr.forEach(ite=>{
										if(parseInt(ite) === 12){
											userRole.proOrderStatu = true;
										}
									})
								}
							}else if(item === '5'){
								companyRole.pop_role = true;
								if(userInfo.type === 0){
									userRole.popOrderStatu = true;
									userRole.countStatu1 = true;
								}else if(userInfo.type === 1){
									role_arr.forEach(ite=>{
										if(parseInt(ite) === 13){
											userRole.popOrderStatu = true;
										}else if(parseInt(ite) === 14){
											userRole.countStatu1 = true;
										}
									})
								}
							}else if(item === '6'){
								companyRole.prop_role = true;
								if(userInfo.type === 0){
									userRole.propOrderStatu = true;
									userRole.countStatu2 = true;
								}else if(userInfo.type === 1){
									role_arr.forEach(ite=>{
										if(parseInt(ite) === 15){
											userRole.propOrderStatu = true;
										}else if(parseInt(ite) === 16){
											userRole.countStatu2 = true;
										}
									})
								}
							}
						})
					}
				}
				this.setCompanyRole(companyRole)
				this.setUserRole(userRole)
			},
			//跳转注册
			toregister(){
				uni.navigateTo({
					url: `/pages/login/register`
				})
			},
			//跳转忘记密码
			toforget() {
				uni.navigateTo({
					url: `/pages/login/forget`
				})
			},
			// 发送验证码
			async sendCode() {
				let resdata = this.formdata;
				if (resdata.phone === "") {
					uni.showToast({
						title: "手机号码不能为空！",
						icon: 'none',
						duration: 2000
					})
					return;
				}
				if (!/(^1[3|4|5|7|8][0-9]{9}$)/.test(resdata.mobile)) {
					uni.showToast({
						title: "请输入正确的手机号码！",
						icon: 'none',
						duration: 2000
					})
					return;
				}
				sendSmss(resdata).then(res =>{
					if(res.code === 2000){
						
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						})
					}
				})
				// 开启倒计时
				this.countDown();
			},
			
			// 验证码倒计时
			countDown() {
				let t = null;
				let timer = 60;
				this.btnDis = true;
				clearInterval(t);
				this.btnText = `${timer}s`;
				t = setInterval(() => {
					if (timer == 0) {
						clearInterval(t);
						this.btnText = "重新发送";
						this.btnDis = false;
						return;
					}
					timer--;
					this.btnText = `${timer}s`;
				}, 1000)
			}
		}
	}
</script>

<style lang="scss">
	.titleNview-placing {
		height: var(--status-bar-height);
		box-sizing: content-box;
		background: $uni-bg-color;
		display: block;
		position: sticky;
		top: 0rpx;
		z-index: 9999;
		overflow: hidden
	}
	.det-con-more {
		display: flex;
		justify-content: center;
		align-items: center;
		margin-bottom: 10rpx;
		color: #bbb;
		font-size: 24rpx;
	}
	.icon-tb {
		width: 32rpx;
		height: 32rpx;
		margin-right: 20rpx;
	}
	.page-content {
		display: flex;
		flex-direction: column;
		background-image: url(/static/login/login_bg.png);
		background-repeat: no-repeat; 
		background-size: 100% 100%; 
		width: 100vw;
		height: 100vh;
		.page-top {
			display: flex;
			flex-direction: column;
			justify-content: center;
			height: 340rpx;
			padding: 160rpx 87rpx 0 87rpx;
			.top-txt{
				font-size: 47rpx;
				font-weight: bold;
				margin-bottom: 30rpx;
				color: $font-color-blue;
			}
		}
		.page-body {
			height: calc(100% - 342rpx);
			.login_input_box {
				display: flex;
				flex: 1;
			
				.input-group {
					display: flex;
					flex: 1;
					flex-direction: column;
					padding:0 75rpx;
			
					.input-row {
						padding: 0 10rpx;
						display: flex;
						align-items: center;
						flex: 1;
						width: 100%;
						height: 84rpx;
						border-bottom: 1rpx solid rgba(191, 191, 191, .75);
						flex-direction: row;
						background: $uni-bg-box-color;
						margin-top: 30rpx;
			
						.input-row-w {
							display: flex;
							flex: 1;
							width: 100%;
							height: 84rpx;
							color: $font-color-3;
							font-size: 28rpx;
						}
					}
					
					.input-row-code {
						width: 100%;
						display: flex;
						align-items: center;
						flex: 1;
						height: 84rpx;
						flex-direction: row;
						background: $uni-bg-box-color;
						margin-top: 30rpx;
						border-bottom: 1rpx solid rgba(210,210,210,.4);
					
						.input-row-code-l {
							flex: 65;
							display: flex;
							align-items: center;
							margin-right: 10rpx;
							.input-row-w {
								display: flex;
								flex: 1;
								width: 100%;
								color: $font-color-3;
								font-size: 28rpx;
							}
						}
					
						.input-row-code-r {
							flex: 35;
							display: flex;
							justify-content: flex-end;
							align-items: flex-end;
					
							button {
								width: 200rpx;
								height: 84rpx;
								line-height: 84rpx;
								padding: 0 10rpx;
								font-size: 28rpx;
								background: (0,0,0,0);
								border-radius: 0rpx;
								color: #fff;
							}
					
						}
					}
			
					.btn-row {
						margin-top: 150rpx;
						padding: 0 10rpx;
			
						button {
							line-height: 100rpx;
							background: $uni-but-bg-color;
							font-size: 36rpx;
							color: $font-color-w;
							height: 100rpx;
							box-shadow: 0rpx 0rpx 10rpx rgba(0, 0, 0, 0.19);
							border-radius: 10rpx;
						}
					}
			
					.psd-reg {
						display: flex;
						justify-content: space-between;
						height: 30rpx;
						line-height: 30rpx;
						font-size: 28rpx;
						margin-top: 30rpx;
						padding: 0 10rpx;
						color: $font-color-b2;
					}
				}
			}
		}
	}
</style>
